package com.cskaoyan.catmall.service.Impl;


import com.cskaoyan.catmall.dao.StatisticsMapper;
import com.cskaoyan.catmall.model.vo.*;

import com.cskaoyan.catmall.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 创建日期: 2022-01-07-11-50
 * author : XGQ
 * description: StatisticsService的实现类
 */
@Component
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public class StatisticsMapperImpl implements StatisticsService {

    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    StatisticsMapper statisticsMapper;

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.UserStatisticsVo
     * @description 用户统计页面显示
     * @author XGQ
     * @date 2022/01/08 11:54
     */
    @Override
    public UserStatisticsVo selectAllByUser() {
        //坐标方向，users为y轴新增用户数量，day为x轴时间
        List<String> colums = new ArrayList<>();
        colums.add("day");
        colums.add("users");

        //获取rows参数
        List<RowsUserVo> rows = new ArrayList<>();
        //获取近七天数据，遍历
        Integer beforeDay = 0;
        Integer afterDay = 0;
        for (int i = 6; i >= 0; i--) {
            beforeDay = i;
            afterDay = i - 1;
            //获取某一天新增用户
            RowsUserVo rowsUser = statisticsMapper.selectAllByUser(beforeDay, afterDay);

            //获取时间
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE, -i); //把日期往后增加一天,整数往后推,负数往前移动
            date = calendar.getTime(); //这个时间就是日期往前推一天的结果

            //输入参数
            rowsUser.setDay(date);

            //增加到数列中
            rows.add(rowsUser);

        }
        //包装，返回相对应的值
        UserStatisticsVo userStatisticsVo = new UserStatisticsVo();
        userStatisticsVo.setColumns(colums);
        userStatisticsVo.setRows(rows);

        return userStatisticsVo;
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.OrderStaticicsVo
     * @description 订单统计页面显示
     * @author XGQ
     * @date 2022/01/08 11:29
     */
    @Override
    public OrderStaticicsVo selectAllByOrder() {
        //columns固定参数
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");

        //获取rows参数
        List<RowsOrderVo> rows = new ArrayList<>();
        //获取近七天数据，遍历
        Integer beforeDay = 0;
        Integer afterDay = 0;
        for (int i = 6; i >= 0; i--) {
            beforeDay = i;
            afterDay = i - 1;
            //获取某一天订单量，总金额，客单价
            RowsOrderVo rowsOrder = statisticsMapper.selectAllByOrder(beforeDay, afterDay);
            //获取下单用户
            Integer customers = statisticsMapper.getCustomersByOrder(beforeDay, afterDay);

            //获取时间
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE, -i); //把日期往后增加一天,整数往后推,负数往前移动
            date = calendar.getTime(); //这个时间就是日期往前推一天的结果

            //输入参数
            rowsOrder.setCustomers(customers);
            rowsOrder.setDay(date);

            //增加到数列中
            rows.add(rowsOrder);
        }

        //包装，返回相对应的值
        OrderStaticicsVo orderStaticicsVo = new OrderStaticicsVo();
        orderStaticicsVo.setColumns(columns);
        orderStaticicsVo.setRows(rows);

        return orderStaticicsVo;
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.GoodsStaticsVo
     * @description 商品统计页面显示
     * @author XGQ
     * @date 2022/01/08 13:24
     */
    @Override
    public GoodsStaticsVo selectAllByGoods() {
        //columns固定参数
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");

        //获取rows参数
        List<RowsGoodsVo> rows = new ArrayList<>();
        //获取近七天数据，遍历
        Integer beforeDay = 0;
        Integer afterDay = 0;
        for (int i = 6; i >= 0; i--) {
            beforeDay = i;
            afterDay = i - 1;
            //获取某一天订单表的订单ID
            List<Integer> ids = statisticsMapper.selectIdByOrder(beforeDay, afterDay);
            Double amount = 0D;
            Integer orders = ids.size();
            Integer products = 0;
            for (Integer orderId : ids) {
                // 根据订单id查找对应订单商品表id
                List<Integer> orderGoodsId = statisticsMapper.getOrderGoodsIdByOrderId(orderId);
                for (Integer id : orderGoodsId) {
                    //获取订单商品的价格数量
                    Double price = statisticsMapper.selectPriceByGoods(id); //商品价格
                    Integer num = statisticsMapper.selectNumByGoods(id); //商品数量
                    amount += (price * num); // 总额
                    products += num; //商品总数量
                }
//                orders++; //订单个数
            }
            //获取时间
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE, -i); //把日期往后增加一天,整数往后推,负数往前移动
            date = calendar.getTime(); //这个时间就是日期往前推一天的结果

            //输入参数
            RowsGoodsVo rowsGoods = new RowsGoodsVo();
            rowsGoods.setDay(date);
            rowsGoods.setOrders(orders);
            rowsGoods.setProducts(products);
            rowsGoods.setAmount(amount);


            //增加到数列中
            rows.add(rowsGoods);
        }

        //包装，返回相对应的值
        GoodsStaticsVo goodsStaticsVo = new GoodsStaticsVo();
        goodsStaticsVo.setColumns(columns);
        goodsStaticsVo.setRows(rows);

        return goodsStaticsVo;

    }
}
